<?php
namespace Common\Model;
use Think\Model;
/**
 * 订单模型
 */
class OrderModel extends Model {
    protected $tableName = 'order_list';

	 public function createUserOrder($bid_id,$bid_cate,$bid_sn){
         switch ($bid_cate) {
             case '1':
                 $user_list=M('bid_user')->where(array('bid_id'=>$bid_id))->order('user_price desc')->select();
                 if(is_array($user_list)){
                     $where['bid_id']=$bid_id;
                     $where['user_id']=$user_list[0]['user_id'];
                     $data=$this->where($where)->find();
                     if(empty($data)){
                         $this->createOrder($bid_id,$bid_cate,$bid_sn,$user_list[0]);
					 }
				 }
                 break;
             case '2':
                 $user_group=M('bid_user')->where(array('bid_id'=>$bid_id))->field('max(per_price) as max_price,id,user_id,good_id,user_number,user_price')->group('good_id')->select();
                 foreach ($user_group as $k=>$v){
                    $this->createOrder($bid_id,$bid_cate,$bid_sn,$v);
				 }
                 break;
         }
	 }

     //创建订单
	 public function createOrder($bid_id,$bid_cate,$bid_sn,$bid_user){
         $where['bid_id']=$bid_id;
         $where['user_id']=$bid_user['user_id'];
         $where['good_id']=$bid_user['good_id'];
         $row=$this->where($where)->find();
         if(empty($row)){
             $data['order_sn']=getOrderSn();
             $data['bid_id']=$bid_id;
             $data['bid_cate']=$bid_cate;
             $data['bid_sn']=$bid_sn;
             $data['good_id']=$bid_user['good_id'];
             $data['user_id']=$bid_user['user_id'];
             $data['good_count']=$bid_user['user_number'];
             $data['total_price']=$bid_user['user_price'];
             $data['create_time']=time();
             $data['status']=1;
             $res=$this->add($data);
             if($res!==false){
                 $row=$this->where(array('id'=>$res))->find();
                 return $row;
             }
             return false;
		 }else{
         	return $row;
		 }

	 }


}
